/**
 *  last update 2014.10.24
 *  Faeple.com
 */
function profile_update(obj) {
    $("#profile-update-overlay").fadeIn();

    $(".fp-update").show();
    $(".fp-display").hide();
    if($("#hasProfileImage").val() == "true" && $("#delProfileImage").val() == "false"){
    	$("#profileImageDelete").show();
    }
    if($("#coverImage").attr("src") != "" && $("#delCoverImage").val() == "false"){
    	$("#coverImageDelete").show();
    }
	$("#topMenu").css("z-index",10);
}

function profile_save(obj) {
	if($("#inputWebsite").val() != "") {
       	var re = new RegExp("^(http|https)://", "i");
       	if(!re.test($("#inputWebsite").val())){
       		$("#inputWebsite").val("http://"+$("#inputWebsite").val());
       	}
	}
    $("#update-form").ajaxSubmit({
        statusCode: {
            400: function () {
                alertShow("danger", "다시 시도해주세요", 2000);
            },
            500: function () {
                alertShow("danger", "다시 시도해주세요", 2000);
            }
        },
        beforeSubmit: function () { $(obj).attr("disabled", "disabled").html("저장중..."); },
        success: function (response) {
        	$(obj).removeAttr("disabled").html("변경사항 저장");
            if (response == "excute") {
                var data = document.getElementById("update-form");
                $("#print_name").html(data.inputName.value);
                $("#print_description").html(data.inputDescription.value);
                if(data.inputWebsite.value != "") {
                	$("#print_website").html("<i class='fa fa-link' style='color: #4E5665; font-size:14px;'></i>&nbsp;<a href='" + data.inputWebsite.value + "' target='_blank'>" + data.inputWebsite.value + "</a>");
                }else{
                	$("#print_website").empty();
                }
                if (data.inputLocation.value == "") {
                    $("#print_location").html(data.inputLocation.value);
                } else {
                    $("#print_location").html(data.inputLocation.value + ", ");
                }
                if($("#prevColor").val() != $("#inputLayoutColor").val()){
                	$(".profile-value").css("color", $("#inputLayoutColor").val());
                	$(".profile-nav").children("li").each(function(){
                		if($(this).hasClass("active")){
                			$(this).css("border-bottom-color", $("#inputLayoutColor").val());
                		}
                	});
                	$(".profile-nav").children("li").hover(function(){
                		$(this).css("border-bottom-color", $("#inputLayoutColor").val());
                	}, function(){
                		
                	});
                	$("#prevColor").val($("#inputLayoutColor").val());
                }
                
                if ($("#movie-upLoadCoverImage").attr("src") != "") {
                    $("div.coverImageWrapper").addClass("cover");
    	        	$("#coverImage").attr("src",$("#movie-upLoadCoverImage").attr("src")).css("top",($("#movie-upLoadCoverImage").height()-$("div.coverImageWrapper").height())*$("#inputCoverImage_top").val()/100).show();
                    $("#delCoverImage").val("false");
                } else {
                	if($("#coverImage").attr("src") == ""){
                		$("div.coverImageWrapper").removeClass("cover");
                	}
                }
                if($("#movie-upLoadProfileImage").attr("src") != ""){
                	$("#hasProfileImage").val("true");
                	$("#profileImage").attr("src",$("#movie-upLoadProfileImage").attr("src")).css("top", $("#inputprofileImage_top").val()+"px").show();
                	$("#profile-imageViewer").attr("href",$("#movie-upLoadProfileImage").attr("src"));
                	if($("#movie-upLoadProfileImage").height() <= 180){
                		$("#profileImage").css("height", 180);
                	}
                }
                $(".fp-update").hide();
    	        $(".fp-display").show();
                $("#profile-update-overlay").fadeOut();
                $("#coverImageDelete").hide();
                $("#profileImageDelete").hide();
                $("#movie-upLoadCoverImage").attr("src", "").css("top", 0).hide();
    	        $("#movie-upLoadProfileImage").attr("src", "").css("top", 0).hide();
    	        alertShow("success", "프로필이 변경되었습니다", 2000);
            } else {
                alertShow("danger", "다시 시도해주세요", 2000);
            }
        }
    });
}

function profile_cancel() {
	$("#topMenu").css("z-index",1000);
    if($("#coverImage").attr("src") == ""){
        $("div.coverImageWrapper").removeClass("cover");
    }
    $(".fp-update").hide();
    $(".fp-display").show();
    $("#profileImageDelete").hide();
	$("#coverImageDelete").hide();
       
    $("#profile-update-overlay").hide();
    $("div.coverImageWrapper").css("background-color", $("#prevColor").val());
    $(".profile-value").css("color", $("#prevColor").val());
    $("#inputLayoutColor").val($("#prevColor").val());
    
    $("#upLoadCoverImage").val("").hide();
    $("#upLoadProfileImage").val("").hide();
    $("#movie-upLoadCoverImage").attr("src", "").css("top", 0).hide();
    $("#movie-upLoadProfileImage").attr("src", "").css("top", 0).hide();
    $("#inputCoverImage_top").val("0");
    $("#inputprofileImage_top").val("0");
    $("#inputName").val($("#print_name").html());
    $("#inputDescription").val($("#print_description").html());
    $("#inputWebsite").val($("#print_website").children("a").html());
    $("#inputLocation").val($("#print_location").html());
}

function profileImageUpload(input, target) {
    var fileList = input.files;
    try {
        var anyWindow = window.URL || window.webkitURL;
        var objectUrl = anyWindow.createObjectURL(fileList[0]);
        if (target == "cover") {
        	$("#upLoadCoverImage").hide();
            $("#movie-upLoadCoverImage").attr("src", objectUrl);
            $("#delCoverImage").val("false");
            $("#coverImageDelete").hide();
            $("#inputCoverImage_top").val("0");
            $("#coverUploadMessage").fadeIn().delay(4000).fadeOut();
        } else {
            $("#movie-upLoadProfileImage").attr("src", objectUrl);
            imagesLoaded("#movie-upLoadProfileImage", function( instance ) {
            	if($("#movie-upLoadProfileImage").height() <= 180){
            		$("#movie-upLoadProfileImage").css("height", 180);
            	}
            });
            $("#profileImageDelete").hide();
            $("#upLoadProfileImage").hide();
            $("#delProfileImage").val("false");
            $("#inputprofileImage_top").val("0");
        }
        //window.URL.revokeObjectURL(fileList[0]);
    } catch (e) {
    	alertShow("danger", "현재 이용중인 인터넷브라우저로는 이미지를 업로드 할 수 없습니다.&nbsp;<a style='color:#401A19;' href='/help?'>자세히 알아보기</a>", null);
    }
}

$(function () {
	$('#inputLayoutColor').minicolors({
        control: 'hue',
        defaultValue: '#E9EAED',
        inline: false,
        position: 'right',
        theme: 'bootstrap',
        change: function (hex) {
            $("div.coverImageWrapper").css("background-color", hex);
            $(".profile-value").css("color", hex);
            if (!hex) return
        }
    });
	
	$("#movie-upLoadCoverImage").draggable({
        scroll: false,
        axis: "y",
        drag: function (event, ui) {
            var h = $("#movie-upLoadCoverImage").height();
            if (ui.position.top >= 0) {
                ui.position.top = 0;
            } else if (ui.position.top <= $("div.coverImageWrapper").height() - h) {
                ui.position.top = $("div.coverImageWrapper").height() - h;
            }
        },
        stop: function (event, ui) {
            $("#inputCoverImage_top").val(Math.round(parseInt(ui.position.top / (($("#movie-upLoadCoverImage").height()-$("div.coverImageWrapper").height()) / 100)+"%")));
        }
    });
    $("#movie-upLoadProfileImage").draggable({
        scroll: false,
        axis: "y",
        drag: function (event, ui) {
            var h = $("#movie-upLoadProfileImage").height();
            if (ui.position.top >= 0) {
                ui.position.top = 0;
            } else if (ui.position.top <= 180 - h) {
                ui.position.top = 180 - h;
            }
        },
        stop: function (event, ui) {
            $("#inputprofileImage_top").val(ui.position.top);
        }
    });
    $("#coverImageDelete").click(function () {
    	$("#coverImage").attr("src", "").hide();
    	$("#movie-upLoadCoverImage").attr("src", "").css("top",0);
        $("#inputCoverImage_top").val("0");
        $("#upLoadCoverImage").val("");
        $("#delCoverImage").val("true");
        $(this).hide();
    });
    $("#profileImageDelete").click(function () {
        $("#profileImage").removeAttr("src").attr("src", "/resources/img/default_profile_pic.png").css("top", 0);
        $("#displayProfileImage").removeAttr("src").attr("src", "/resources/img/default_profile_pic.png");
        $("#upLoadCoverImage").val("");
        $("#delProfileImage").val("true");
        $("#hasProfileImage").val("false");
        $("#inputprofileImage_top").val("0");
        $(this).hide();
    });
});